using System;
using System.Data;
using System.Data.Objects;

namespace Salient.Web.SQLite.Model
{
    public partial class AspNetDbEntities
    {
        // SavingChanges event handler.
        private static void context_SavingChanges(object sender, EventArgs e)
        {
            // Validate the state of each entity in the context
            // before SaveChanges can succeed.
            foreach (ObjectStateEntry entry in
                ((ObjectContext)sender).ObjectStateManager.GetObjectStateEntries(EntityState.Added |
                                                                                 EntityState.Modified))
            {
                // Find an object state entry for a SalesOrderHeader object. 
                if (!entry.IsRelationship && (entry.Entity.GetType() == typeof(Application)))
                {
                    Application ent = entry.Entity as Application;
                    if (ent != null && ent.ApplicationId == Guid.Empty)
                    {
                        ent.ApplicationId = Guid.NewGuid();
                    }
                }

                if (!entry.IsRelationship && (entry.Entity.GetType() == typeof(PersonalizationPath)))
                {
                    PersonalizationPath ent = entry.Entity as PersonalizationPath;
                    if (ent != null && ent.PathId == Guid.Empty)
                    {
                        ent.PathId = Guid.NewGuid();
                    }
                }

                if (!entry.IsRelationship && (entry.Entity.GetType() == typeof(UserPath)))
                {
                    UserPath ent = entry.Entity as UserPath;
                    if (ent != null && ent.Id == Guid.Empty)
                    {
                        ent.Id = Guid.NewGuid();
                    }
                }

                if (!entry.IsRelationship && (entry.Entity.GetType() == typeof(Role)))
                {
                    Role ent = entry.Entity as Role;
                    if (ent != null && ent.RoleId == Guid.Empty)
                    {
                        ent.RoleId = Guid.NewGuid();
                    }
                }

                if (!entry.IsRelationship && (entry.Entity.GetType() == typeof(AspUser)))
                {
                    AspUser ent = entry.Entity as AspUser;
                    if (ent != null && ent.UserId == Guid.Empty)
                    {
                        ent.UserId = Guid.NewGuid();
                    }
                }
            }
        }

        partial void OnContextCreated()
        {
            SavingChanges += context_SavingChanges;
        }
    }
}